package com.xframe.xdal.core.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * Base64类 <br>
 * 功能：字符串的BASE64编码解码。
 */

public class Base64Util {
    /**
     * 将字符串转化为base64编码
     *
     * @param str
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String encodeStr(String str)
            throws UnsupportedEncodingException {
        if (StringUtil.isEmpty(str)) {
            str = "";
        }
        byte[] bytes = Base64.encodeBase64(str.getBytes(StandardCharsets.UTF_8));
        return new String(bytes, StandardCharsets.UTF_8);

    }

    /**
     * 将Base64编码的字符串解码
     *
     * @param s
     * @return
     * @throws
     */
    public static byte[] decodeBase64(String s) {
        if (s != null) {
            byte[] bytes;
            bytes = s.getBytes(StandardCharsets.UTF_8);
            return Base64.decodeBase64(bytes);
        }
        return null;
    }
}
